package com.flot.yazb.widget.utils;

/**
 * Created by fengdongfei on 2017/10/12.
 */

public class EncodeUtils {
    public static int keyStore=3;
//    public static byte[] encrypt1(byte[] bytes) {
//        if (bytes == null) {
//            return null;
//        }
//        int len = bytes.length;
//        int key = 0x12;
//        for (int i = 0; i < len; i++) {
//            bytes[i] ^= key;
//        }
//        return bytes;
//    }

    public static byte[] encrypt(byte[] bytes){
        if (bytes == null){
            return null;
        }

        int len = bytes.length;
        int key = keyStore;
        for (int i = 0; i< len; i++){
            bytes[i] += key;
        }

        return bytes;
    }

    public static byte[] decrypt(byte[] bytes){
        if (bytes == null){
            return null;
        }

        int len = bytes.length;
        int key = keyStore;
        for (int i = 0; i< len; i++){
            bytes[i] -= key;
        }
        return bytes;
    }



    public static void main(String[] args) {
//        byte[] bytes = encrypt("-=+~O(∩_∩)O哈哈~Hello+=-?><,./;':\\\"\\\\_)(0987654321`~!@#$%^&*()_+\t|".getBytes());//加密
//        System.out.println(Arrays.toString(bytes));//字节数组打印
//        String str1 = new String(encrypt(bytes));//解密
//        System.out.println(str1);

//        byte[] bytes = "O哈哈this/;':\\\\\\\"\\\\\\\\_)(0987654   321`~!@#$%^".getBytes();
//        //先base64加密,再移位,再base64加密
//        String str = Base64Utils.encode(bytes);
//        String encodeing = new String(encrypt(str.getBytes()));
//        String encodestr = Base64Utils.encode(encodeing.getBytes());
//        System.out.println(encodestr);


        //先base64解密,再移位,再base64解密
        byte[] bytes1 = Base64Utils.decode("aHxNd11bUX1cWmdvTG1ybGY2WW1cNVl9ZnxMdkxwUXldSlhsUm1MelBGemxdSkkzXFZMOWVxWXZlSzNA");
        String str2 = new String(decrypt(bytes1));
        byte[] bytes2 = Base64Utils.decode(str2);
        String str3 = new String(bytes2);
        System.out.println(str3);
    }

    public static String encp(String sc){
        String str = Base64Utils.encode(sc.getBytes());
        String encodeing = new String(encrypt(str.getBytes()));
        String encodestr = Base64Utils.encode(encodeing.getBytes());
        return encodestr;
    }

    public static String decp(String ec){
        byte[] bytes1 = Base64Utils.decode(ec);
        String str2 = new String(decrypt(bytes1));
        byte[] bytes2 = Base64Utils.decode(str2);
        String str3 = new String(bytes2);
        return str3;
    }
}
